package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// Conversation holds the schema definition for the Conversation entity.
type Conversation struct {
	ent.Schema
}

// Fields of the Conversation.
func (Conversation) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable().
			Comment("会话ID"),
		field.Enum("type").
			Values("private", "group").
			Comment("会话类型"),
		field.String("name").
			Optional().
			MaxLen(100).
			Comment("会话名称"),
		field.Int64("creator_id").
			Comment("创建者ID"),
		field.String("avatar_url").
			Optional().
			MaxLen(255).
			Comment("会话头像URL"),
		field.Text("description").
			Optional().
			Comment("会话描述"),
		field.Time("created_at").
			Default(time.Now).
			Immutable().
			Comment("创建时间"),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now).
			Comment("更新时间"),
		field.Time("last_message_at").
			Optional().
			Comment("最后消息时间"),
	}
}

// Edges of the Conversation.
func (Conversation) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("creator", User.Type).
			Ref("created_conversations").
			Field("creator_id").
			Unique().
			Required().
			Comment("会话创建者"),
		edge.To("members", ConversationMember.Type).
			Comment("会话成员"),
		edge.To("messages", Message.Type).
			Comment("会话消息"),
	}
}

// Indexes of the Conversation.
func (Conversation) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("type"),
		index.Fields("creator_id"),
		index.Fields("last_message_at"),
	}
}
